import ReconnectingWebSocket from 'reconnecting-websocket'
// let getIp = window.location.href.split('/')[2].split(':')[0]
let getIp = window.location.href.split('/')[2].split(':')[0]

// const socket = new WebSocket(`ws://${getIp}:8080/websocket`)

let host = process.env.NODE_ENV !== 'production' ? '192.168.0.135' : getIp

let getPort = window.location.href.split('/')[2].split(':')[1]

let port = getPort || '80'
// let port = '8060'

const options = {
  connectionTimeout: 4000,
  maxRetries: Infinity
}

let wsUrl = process.env.NODE_ENV !== 'production'
  ? `ws://${host}/websocket/` : `ws://${host}:${port}/websocket/`

const rws = new ReconnectingWebSocket(wsUrl, [], options)

// console.log(rws)

const logMsgType = [501, 502, 506, 519, 520, 521, 522, 523, 524, 527, 530, 532, 533, 534, 535, 536, 537, 538, 539, 540, 541, 542, 543, 603, 604, 605, 606, 607]

function createWebSocketPlugin (rws) {
  return store => {
    rws.addEventListener('open', () => {
      // rws.send('hello!')
      console.log('rws connected')
    })
    rws.onmessage = event => {
      let data = JSON.parse(event.data)
      // store.commit('receiveWebSocket', data)
      // let consoleMsgType = [607]
      // if (consoleMsgType.includes(data.msg_type)) {
      //   console.log(data)
      // }
      // 记录日志
      if (logMsgType.includes(data.msg_type)) {
        store.commit('pushEventLog', {
          time: data.event_time,
          msg_type: data.msg_type,
          event_data: event.data
        })
      }
      // 数据同步
      switch (data.msg_type) {
        case 501:
          // 入网
          store.commit('setStationState', {
            id: data.station_id,
            state: data.station_state
          })
          store.commit('setCuState', {
            device: data.cu_device,
            state: data.cu_state
          })
          // openLayers 相关
          store.commit('setStationStateForOpenLayers', [
            {
              id: data.station_id,
              state: data.station_state
            }
          ])
          store.commit('pushNetworkAccessLine', { id: data.station_id })
          break
        case 506:
          // 退网
          store.commit('setStationState', {
            id: data.station_id,
            state: data.station_state
          })
          store.commit('setCuState', {
            device: data.cu_device,
            state: data.cu_state
          })
          // 12-18临时
          if (data.cu_state === 0) {
            store.commit('cuLogOutNotify', {
              station_id: data.station_id,
              device: data.cu_device,
              state: data.cu_state
            })
          }
          // openLayers 相关
          store.commit('setStationStateForOpenLayers', [
            {
              id: data.station_id,
              state: data.station_state
            }
          ])
          store.commit('deleteNetworkAccessLine', { id: data.station_id })
          break
        case 521:
          // 通信
          store.commit('pushUsingTrafficChannel', data)
          store.commit('pushStationMapLine', data)
          store.commit('setStationState', {
            id: data.caller_station_id,
            state: data.caller_station_state
          })
          store.commit('setStationState', {
            id: data.called_station_id,
            state: data.called_station_state
          })
          store.commit('setCuState', {
            device: data.caller_cu_device,
            state: data.caller_cu_state
          })
          store.commit('setCuState', {
            device: data.called_cu_device,
            state: data.called_cu_state
          })
          // openLayers 相关
          store.commit('setStationStateForOpenLayers', [
            {
              id: data.caller_station_id,
              state: data.caller_station_state
            },
            {
              id: data.called_station_id,
              state: data.called_station_state
            }
          ])
          break
        case 603:
          // 接入成功事件，同通信成功521事件处理方式，
          store.commit('pushUsingTrafficChannel', data)
          store.commit('pushStationMapLine', data)
          store.commit('setStationState', {
            id: data.caller_station_id,
            state: data.caller_station_state
          })
          store.commit('setStationState', {
            id: data.called_station_id,
            state: data.called_station_state
          })
          store.commit('setCuState', {
            device: data.caller_cu_device,
            state: data.caller_cu_state
          })
          store.commit('setCuState', {
            device: data.called_cu_device,
            state: data.called_cu_state
          })
          // openLayers 相关
          store.commit('setStationStateForOpenLayers', [
            {
              id: data.caller_station_id,
              state: data.caller_station_state
            },
            {
              id: data.called_station_id,
              state: data.called_station_state
            }
          ])
          break
        case 522:
          // 通信结束
          store.commit('deleteUsingTrafficChannel', data)
          store.commit('deleteStationMapline', data)
          store.commit('setStationState', {
            id: data.caller_station_id,
            state: data.caller_station_state
          })
          store.commit('setStationState', {
            id: data.called_station_id,
            state: data.called_station_state
          })
          store.commit('setCuState', {
            device: data.caller_cu_device,
            state: data.caller_cu_state
          })
          store.commit('setCuState', {
            device: data.called_cu_device,
            state: data.called_cu_state
          })
          // openLayers 相关
          store.commit('setStationStateForOpenLayers', [
            {
              id: data.caller_station_id,
              state: data.caller_station_state
            },
            {
              id: data.called_station_id,
              state: data.called_station_state
            }
          ])
          break
        case 604:
          // 接入结束事件，与通信结束事件521做相同处理。
          store.commit('deleteUsingTrafficChannel', data)
          store.commit('deleteStationMapline', data)
          store.commit('setStationState', {
            id: data.caller_station_id,
            state: data.caller_station_state
          })
          store.commit('setStationState', {
            id: data.called_station_id,
            state: data.called_station_state
          })
          store.commit('setCuState', {
            device: data.caller_cu_device,
            state: data.caller_cu_state
          })
          store.commit('setCuState', {
            device: data.called_cu_device,
            state: data.called_cu_state
          })
          // openLayers 相关
          store.commit('setStationStateForOpenLayers', [
            {
              id: data.caller_station_id,
              state: data.caller_station_state
            },
            {
              id: data.called_station_id,
              state: data.called_station_state
            }
          ])
          break
        case 605:
          // 接入状态变更
          store.commit('deleteUsingTrafficChannel', data)
          store.commit('deleteStationMapline', data)
          store.commit('pushUsingTrafficChannel', data)
          store.commit('pushStationMapLine', data)
          store.commit('setStationState', {
            id: data.caller_station_id,
            state: data.caller_station_state
          })
          store.commit('setStationState', {
            id: data.called_station_id,
            state: data.called_station_state
          })
          store.commit('setCuState', {
            device: data.caller_cu_device,
            state: data.caller_cu_state
          })
          store.commit('setCuState', {
            device: data.called_cu_device,
            state: data.called_cu_state
          })
          // openLayers 相关
          store.commit('setStationStateForOpenLayers', [
            {
              id: data.caller_station_id,
              state: data.caller_station_state
            },
            {
              id: data.called_station_id,
              state: data.called_station_state
            }
          ])
          break
        case 523:
          // 地球站位置上报
          store.commit('changeStationCoords', {
            station_id: data.station_id,
            longitude: data.longitude,
            latitude: data.latitude
          })
          store.commit('changeStationMapLineCoords', {
            station_id: data.station_id,
            longitude: data.longitude,
            latitude: data.latitude
          })
          store.commit('pushNetworkAccessLine', {
            id: data.station_id
          })
          break
        case 524:
          // ccu通道状态变化   0关闭  1开启
          if (data.state === 0) {
            store.commit('deleteUsingCtrlChannel', data)
          } else if (data.state === 1) {
            store.commit('pushUsingCtrlChannel', data)
          }
          break
        case 525:
          // 控制信令计数
          store.commit('pushCtrlCount', {
            order_count: data.order_count
          })
          // store.commit('setCtrlSignalCount', {
          //   tdm_count: data.tdm_count,
          //   aloha_count: data.aloha_count
          // })
          break
        case 526:
          // 信道利用率
          store.commit('setChannelUtility', {
            rate_tdm: data.tdm_rate,
            rate_aloha: data.aloha_rate,
            rate_traffic: data.trfc_rate
          })
          break
        case 527:
          // 控制终端状态变化
          store.commit('setCcuListNcpState', {
            id: data.ccu_id,
            is_connected: data.state === 1
          })
          break
        case 530:
          // 资源更新
          store.dispatch(`set530Event`, data)
          store.commit('setCuState', {
            device: data.caller_cu_device,
            state: data.caller_cu_state
          })
          store.commit('setCuState', {
            device: data.called_cu_device,
            state: data.called_cu_state
          })
          break
        case 531:
          // 信噪比
          store.commit('setCuSnr', data)
          break
        case 532:
          // 预约点对点通信
          // freqline
          store.commit('pushUsingSubscribeFreqSegment', {
            id: data.subscribe_id,
            ...data
          })
          // mapline
          store.commit('pushStationMapLine', data)
          break
        case 533:
          // 终端加入预约
          // stationState
          store.commit('setStationState', {
            id: data.station_id,
            state: 2
          })
          // custate
          store.commit('setCuState', {
            device: data.cu_device,
            state: 2
          })
          break
        case 534:
          // 终端退出预约
          // stationState
          store.commit('setStationState', {
            id: data.station_id,
            state: data.station_state
          })
          // custate
          store.commit('setCuState', {
            device: data.cu_device,
            state: 1
          })
          break
        case 535:
          // 预约结束
          // freqline
          store.commit('deleteUsingSubscribeFreqSegment', {
            id: data.subscribe_id,
            ...data
          })
          // mapline
          store.commit('deleteStationMapline', data)
          break
        case 536:
          // 组播开始事件
          // freqline
          store.commit('pushUsingMulticastFreqSegment', {
            id: data.multicast_id,
            ...data
          })
          for (let item of data.back_passes) {
            store.commit('pushUsingMulticastFreqSegment', {
              id: data.multicast_id,
              ...item
            })
          }
          // mapline
          break
        case 537:
          // 终端开始组播通信事件
          store.commit('setStationState', {
            id: data.station_id,
            state: 2
          })
          store.commit('setCuState', {
            device: data.cu_device,
            state: 2
          })
          break
        case 538:
          // 终端结束组播通信事件
          store.commit('setStationState', {
            id: data.station_id,
            state: data.station_state
          })
          store.commit('setCuState', {
            device: data.cu_device,
            state: 1
          })
          break
        case 539:
          // 组播结束事件
          // freqline
          store.commit('deleteUsingMulticastFreqSegment', {
            id: data.multicast_id,
            ...data
          })
          // mapline
          break
        case 540:
          // 终端加入组播事件
          // mapline
          store.commit('pushStationMapLine', {
            caller_station: data.sender_station,
            called_station: data.receive_station
          })
          // stationState
          store.commit('setStationState', {
            id: data.sender_station,
            state: 2
          })
          store.commit('setStationState', {
            id: data.receive_station,
            state: 2
          })
          // custate
          store.commit('setCuState', {
            device: data.sender_cu_device,
            state: 2
          })
          store.commit('setCuState', {
            device: data.receive_cu_device,
            state: 2
          })
          break
        case 541:
          // 终端退出组播事件
          // mapline
          store.commit('deleteStationMapline', {
            caller_station: data.sender_station,
            called_station: data.receive_station
          })
          // stationState
          store.commit('setStationState', {
            id: data.sender_station,
            state: data.sender_station_state
          })
          store.commit('setStationState', {
            id: data.receive_station,
            state: data.receive_station_state
          })
          // custate
          store.commit('setCuState', {
            device: data.sender_cu_device,
            state: 1
          })
          store.commit('setCuState', {
            device: data.receive_cu_device,
            state: 1
          })
          break
        case 542:
          // 终端增加回传
          // freqline
          store.commit('pushUsingMulticastFreqSegment', {
            id: data.multicast_id,
            ...data
          })
          // mapline
          store.commit('pushStationMapLine', {
            caller_station: data.receive_station,
            called_station: data.back_pass_receive_station
          })
          // stationState
          store.commit('setStationState', {
            id: data.receive_station_state,
            state: 2
          })
          store.commit('setStationState', {
            id: data.back_pass_receive_station_state,
            state: 2
          })
          // custate
          store.commit('setCuState', {
            device: data.receive_cu_device,
            state: 2
          })
          store.commit('setCuState', {
            device: data.back_pass_receive_cu_device,
            state: 2
          })
          break
        case 543:
          // 组播回传关闭
          // freqline
          store.commit('deleteUsingMulticastFreqSegment', {
            id: data.multicast_id,
            ...data
          })
          // mapline
          store.commit('deleteStationMapline', {
            caller_station: data.receive_station,
            called_station: data.back_pass_receive_station
          })
          // stationState
          store.commit('setStationState', {
            id: data.receive_station,
            state: data.receive_station_state
          })
          store.commit('setStationState', {
            id: data.back_pass_receive_station,
            state: data.back_pass_receive_station_state
          })
          // custate
          store.commit('setCuState', {
            device: data.receive_cu_device,
            state: 1
          })
          store.commit('setCuState', {
            device: data.back_pass_receive_cu_device,
            state: 1
          })
          break
        case 602:
          store.commit('SET_RCU_LIST', data.rcus)
          break
        case 607:
          // sam状态变化
          store.commit('setStationState', {
            id: data.center_station_id,
            state: data.center_station_state
          })
          break
        default:
          break
      }
    }
  }
}

const webSocketPlugin = createWebSocketPlugin(rws)
export default webSocketPlugin
